class login{
    constructor(){
        this.bindEve()
    }
    bindEve(){//给登录按钮绑定事件
        this.$('.over').addEventListener('click', this.overFn.bind(this))
    }
    overFn(){
    let forms = document.forms[0].elements;
    //  console.log(forms);
    let username = forms.uname.value;
    let password = forms.password.value
    //非空验证
     if (!username.trim() || !password.trim()) throw new Error('账号密码不能为空');
    //  console.log(username,password);
     axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
     let data = `username=${username}&password=${password}`;
     axios.post('http://localhost:8888/users/login', data).then(res => {
      //  console.log(data);
     let { status, data } = res;
    //  console.log(data);
     if (status == 200) {
         if (data.code == 1) {
          localStorage.setItem('token', data.token);
          localStorage.setItem('user_id', data.user.id);      
           location.assign(location.search==''?location.search='index.html':location.search
           
           
           .split('=')[1]);
          alert('登录成功')
         
          }  else {
          layer.open({
            title: '登录提示'
            , content: '用户名或者密码输入错误'
          });
        }
      }
    })
     }
    $(ele) {
        let res = document.querySelectorAll(ele)
        return res.length == 1 ? res[0] : res;
      }
}
new login